package com.iocaop.uaa.security.UserDetails;

import com.iocaop.uaa.repository.UserRepo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserRepo userRepo;

    /**
     * 根据用户名查询 UserDetails
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userRepo.findOptionalByUsername(username)
            .orElseThrow(()->new UsernameNotFoundException("用户名未找到:" + username));
    }
}
